﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Collections.Specialized;
using BfLib.Text;

namespace BfLib.IO
{
    public static class FileLib
    {
        public static string GetTextFromFile(string filename)
        {
            string result = string.Empty;

            using (StreamReader sr = new StreamReader(filename))
            {
                result = sr.ReadToEnd();
            }
            
            return result;
        }

        public static string CreateTempFile(bool MakeHidden, string FileExtension)
        {
            if (FileExtension == null) FileExtension = ".tmp";
            if (FileExtension.Length == 0) FileExtension = ".tmp";
            if (FileExtension[0] != '.') FileExtension = "." + FileExtension;

            string tempFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string fileName = String.Format("{0}\\~{1}{2}", tempFolder, StringLib.GetRandomString(10), FileExtension);

            while (File.Exists(fileName))
            {
                fileName = String.Format("{0}\\~{1}{2}", tempFolder, StringLib.GetRandomString(10), FileExtension);
            }

            try
            {
                using (FileStream fs = File.Create(fileName)) 
                { 
                
                }

                if (MakeHidden)
                {
                    FileAttributes attributes = File.GetAttributes(fileName);
                    File.SetAttributes(fileName, attributes | FileAttributes.Hidden);
                }
            }
            catch
            {
                return string.Empty;
            }

            return fileName;
        }

        #region Read File Attributes

        public static bool FileIsReadOnly(string Filename)
        {
            return ((File.GetAttributes(Filename) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
        }

        public static bool FileIsArchived(string Filename)
        {
            return ((File.GetAttributes(Filename) & FileAttributes.Archive) == FileAttributes.Archive);
        }

        public static bool FileIsHidden(string Filename)
        {
            return ((File.GetAttributes(Filename) & FileAttributes.Hidden) == FileAttributes.Hidden);
        }

        public static bool FileIsSystem(string Filename)
        {
            return ((File.GetAttributes(Filename) & FileAttributes.System) == FileAttributes.System);
        }

        public static bool FileTimeIsCoherent(string Filename)
        {
            FileInfo fi = new FileInfo(Filename);
            return ((fi.CreationTime <= fi.LastWriteTime) && (fi.CreationTime <= fi.LastAccessTime) && (fi.LastWriteTime <= fi.LastAccessTime));
        }

        #endregion

        public static bool LoadFileList(string DirectoryName, string FileMask, ref string[] FileList, bool IncludeSubFolders)
        {
            if(DirectoryName==null) return false;
            if(!Directory.Exists(DirectoryName)) return false;

            SearchOption so;
            if(IncludeSubFolders)
                so = SearchOption.AllDirectories;
            else
                so = SearchOption.TopDirectoryOnly;

            FileList = Directory.GetFiles(DirectoryName, FileMask, so);
            return true;
        }
    }
}
